package com.hornsun.repository;

import com.google.common.base.Preconditions;
import com.hornsun.util.StrUtil;
import com.hornsun.domain.dbo.Evaluate;
import com.hornsun.domain.dbo.EvaluateExample;
import com.hornsun.repository.mapper.EvaluateMapper;
import com.hornsun.util.LittleUtil;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 编译中... on 2018/5/3.
 */
@Repository
public class EvaluateRepository {
    @Resource
    private EvaluateMapper evaluateMapper;

    @Resource
    private LittleUtil littleUtil;

    public List<Evaluate> selectEvaluate(String commodityGuid ,int page){
        Preconditions.checkNotNull(commodityGuid);
        Preconditions.checkNotNull(page);
        EvaluateExample evaluateExample = new EvaluateExample();
        evaluateExample.setOrderByClause(StrUtil.toDesc("time"));
        EvaluateExample.Criteria criteria = evaluateExample.createCriteria();
        criteria.andCommodityGuidEqualTo(commodityGuid);
        return evaluateMapper.selectByExampleWithRowbounds(evaluateExample,littleUtil.littleRowBounds(page) );
    }

    public void insertEvaluate(Evaluate evaluate){
        int count = evaluateMapper.insertSelective(evaluate);
        if (count!=1){
            throw new IllegalArgumentException("insert evaluate failed by evaluate:"+evaluate);
        }
    }

    public long getTotalCount(String commodityGuid){
        Preconditions.checkNotNull(commodityGuid);
        EvaluateExample evaluateExample = new EvaluateExample();
        EvaluateExample.Criteria criteria = evaluateExample.createCriteria();
        criteria.andCommodityGuidEqualTo(commodityGuid);
        return evaluateMapper.countByExample(evaluateExample);
    }

    public double getAVGPoint(String commodityGuid){
        Preconditions.checkNotNull(commodityGuid);
        return evaluateMapper.getAVGPoint(commodityGuid);
    }
}
